package com.epam.facebook_client;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.epam.facebook_client.content.contract.AppContract;
import com.epam.facebook_client.content.db.DbOpenHelper;
import com.epam.facebook_client.manager.ImageManager;

public class FeedActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {

	private long feed_id;
	private ImageManager imageManager;
	private TextView feedTypeTextView;
	private TextView feedStoryTextView;
	private TextView feedMassageTextView;
	private TextView feedUserTextView;
	private TextView feedDescriptionTextView;
	private ImageView feedImageView;
	private Button openLinkButton;

	//TODO fix layout
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feed);
		imageManager = ImageManager.get(this);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			feed_id = extras.getLong(ListFeedActivity.FEED_ID);
		} else {
			finish();
		}
		feedTypeTextView = (TextView) findViewById(R.id.feedType);
		feedStoryTextView = (TextView) findViewById(R.id.feedStory);
		feedMassageTextView = (TextView) findViewById(R.id.feedMessage);
		feedUserTextView = (TextView) findViewById(R.id.feedUser);
		feedDescriptionTextView = (TextView) findViewById(R.id.feedDescription);
		feedImageView = (ImageView) findViewById(R.id.feedImage);
		openLinkButton = (Button) findViewById(R.id.openLinkButton);
		openLinkButton.setVisibility(View.GONE);
	}

	@Override
	protected void onStart() {
		super.onStart();
		getSupportLoaderManager().initLoader(0, null, this);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		String[] selectArgs = { String.valueOf(feed_id) };
		return new CursorLoader(this, AppContract.FeedColumns.CONTENT_URI, null, "_id = ?", selectArgs, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		if (cursor.getCount() == 1) {
			show(cursor);
		}
	}

	private void show(Cursor cursor) {
		cursor.moveToFirst();
		String picture = cursor.getString(DbOpenHelper.FEEDS_PICTURE_COL_INDEX);
		if (picture == null || picture.equals("")) {
			feedImageView.setVisibility(View.GONE);
		} else {
			imageManager.loadImage(picture, feedImageView);
		}
		String name = cursor.getString(DbOpenHelper.FEEDS_NAME_COL_INDEX);
		feedUserTextView.setText(name);
		String type = cursor.getString(DbOpenHelper.FEEDS_TYPE_COL_INDEX);
		feedTypeTextView.setText(getString(R.string.type, type));
		String story = cursor.getString(DbOpenHelper.FEEDS_STORY_COL_INDEX);
		if (story == null || story.equals("")) {
			feedStoryTextView.setVisibility(View.GONE);
		} else {
			feedStoryTextView.setText(getString(R.string.story, story));
		}
		String message = cursor.getString(DbOpenHelper.FEEDS_MESSAGE_COL_INDEX);
		if (message == null || message.equals("")) {
			feedMassageTextView.setVisibility(View.GONE);
		} else {
			feedMassageTextView.setText(getString(R.string.message, message));
		}
		String description = cursor.getString(DbOpenHelper.FEEDS_DESCRIPTION_COL_INDEX);
		if (description == null || description.equals("")) {
			feedDescriptionTextView.setVisibility(View.GONE);
		} else {
			feedDescriptionTextView.setText(getString(R.string.description, description));
		}
		final String link = cursor.getString(DbOpenHelper.FEEDS_LINK_COL_INDEX);
		if (link != null && !link.equals("")) {
			openLinkButton.setVisibility(View.VISIBLE);
			openLinkButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Uri uri = Uri.parse(link);
					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
					startActivity(intent);
				}
			});
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {

	}
}
